import xml.etree.ElementTree as ET
from mpclient import Client
from template import *
from Panda.Core.Log import logger


class BotApi:

    __prog__ = (
        "ToUserName",
        "FromUserName",
        "CreateTime",
        "MsgType",
        "Content",
        "MsgId",
    )

    def __init__(self, client: Client, xml_data):
        self._xml_data = ET.fromstring(xml_data)
        self.ToUserName = self._xml_data.find("ToUserName").text
        self.FromUserName = self._xml_data.find("FromUserName").text
        self.CreateTime = self._xml_data.find("CreateTime").text
        self.MsgType = self._xml_data.find("MsgType").text
        self.Content = self._xml_data.find("Content").text
        self.MsgId = self._xml_data.find("MsgId").text
        self.Idx = self._idx.text if self._xml_data.find("Idx") is not None else None
        self.data_dict: MessageDict = MessageDict(self.__dict__())
        self.client: Client = client

    def __dict__(self):
        return {
            item: str(getattr(self, item))
            for item in self.__prog__
            if not item.startswith("_")
        }

    def __repr__(self):
        return str(
            {
                item: str(getattr(self, item))
                for item in self.__prog__
                if not item.startswith("_")
            }
        )

    @property
    def text(self):
        return self.data_dict.content
